package com.ycsj.common.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Build;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.github.zackratos.ultimatebar.UltimateBar;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * 状态栏工具类
 * Created by Administrator on 2018/10/15 0015.
 */

public class StateBarUtil {
    /*
    * 注意:该工具类中的所有传入的颜色值需要通过
    * getResources().getColor(R.color.colorId)传入
    */

    /**
     * 自定义状态栏和导航栏颜色的方法(非沉浸效果)
     *
     * @param activity      上下文
     * @param stateBarColor 状态栏颜色
     * @param navBarColor   导航栏颜色
     */
    public static void customStateBarAndNavBarColors(Activity activity, int stateBarColor, int navBarColor) {
        UltimateBar.newColorBuilder()
                .statusColor(stateBarColor)//状态栏颜色
//                .statusDepth(50)        //状态栏颜色深度
                .applyNav(true)         //是否应用到导航栏
                .navColor(navBarColor) //导航栏颜色
//                .navDepth(50)           //导航栏颜色深度
                .build(activity)
                .apply();
    }

    /**
     * 自定义状态栏和导航栏颜色的方法(非沉浸效果)
     *
     * @param activity      上下文
     * @param stateBarColor 状态栏颜色
     */
    public static void customStateBar(Activity activity, int stateBarColor) {
        UltimateBar.newColorBuilder()
                .statusColor(stateBarColor)//状态栏颜色
//                .statusDepth(50)        //状态栏颜色深度
                .applyNav(false)         //是否应用到导航栏
//                .navDepth(50)           //导航栏颜色深度
                .build(activity)
                .apply();
    }


    /**
     * 自定义沉浸式状态栏(沉浸效果)
     */
    public static void customImmerseBar(Activity activity) {
        UltimateBar.newImmersionBuilder()
                .applyNav(false)         // 是否应用到导航栏
                .build(activity)
                .apply();
    }


    /*-----------------------------补充:设置半透明的沉浸效果状态栏和导航栏的方法----------------------------------------*/

    /**
     * 自定义半透明状态栏和导航栏(具备沉浸式效果)
     *
     * @param activity      上下文
     * @param stateBarColor 状态栏颜色
     * @param navBarColor   导航栏颜色
     */
    public static void customHalfTransparentBar(Activity activity, int stateBarColor, int navBarColor) {
        UltimateBar.newTransparentBuilder()
                .statusColor(stateBarColor)        // 状态栏颜色
//                .statusAlpha(100)               // 状态栏透明度
                .applyNav(false)                 // 是否应用到导航栏
                .navColor(navBarColor)          // 导航栏颜色
//                .navAlpha(100)                  // 导航栏透明度
                .build(activity)
                .apply();
    }


    /*------------------------------------设置状态栏字体颜色为黑色的方法,解决了MIUI和魅族手机不能变黑色字体的方法--------------------------------------------------------*/
    private static boolean isMiUi = false;

    /*
     * 静态域，获取系统版本是否基于MIUI
     */

    static {
        try {
            @SuppressLint("PrivateApi") Class<?> sysClass = Class.forName("android.os.SystemProperties");
            Method getStringMethod = sysClass.getDeclaredMethod("get", String.class);
            String version = (String) getStringMethod.invoke(sysClass, "ro.miui.ui.version.name");
            isMiUi = version.compareTo("V6") >= 0 && Build.VERSION.SDK_INT < 24;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 设置小米黑色状态栏字体
     */
    @SuppressLint("PrivateApi")
    private static void setMIUIStatusBarDarkMode(Activity activity) {
        if (isMiUi) {
            Class<? extends Window> clazz = activity.getWindow().getClass();
            try {
                int darkModeFlag;
                Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
                Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
                darkModeFlag = field.getInt(layoutParams);
                Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
                extraFlagField.invoke(activity.getWindow(), darkModeFlag, darkModeFlag);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 设置魅族手机状态栏图标颜色风格
     * 可以用来判断是否为Flyme用户
     *
     * @param window 需要设置的窗口
     * @param dark   是否把状态栏字体及图标颜色设置为深色
     * @return boolean 成功执行返回true
     */
    public static boolean setMeiZuDarkMode(Window window, boolean dark) {
        boolean result = false;
        if (Build.VERSION.SDK_INT >= 24) {
            return false;
        }
        if (window != null) {
            try {
                WindowManager.LayoutParams lp = window.getAttributes();
                Field darkFlag = WindowManager.LayoutParams.class
                        .getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
                Field meizuFlags = WindowManager.LayoutParams.class
                        .getDeclaredField("meizuFlags");
                darkFlag.setAccessible(true);
                meizuFlags.setAccessible(true);
                int bit = darkFlag.getInt(null);
                int value = meizuFlags.getInt(lp);
                if (dark) {
                    value |= bit;
                } else {
                    value &= ~bit;
                }
                meizuFlags.setInt(lp, value);
                window.setAttributes(lp);
                result = true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    @SuppressLint("InlinedApi")
    private static int getStatusBarLightMode(Activity activity) {
        int result = 0;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (isMiUi) {
                result = 1;
            } else if (setMeiZuDarkMode(activity.getWindow(), true)) {
                result = 2;
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                result = 3;
            }
        }
        return result;
    }

    /**
     * 直接调用该方法即可设置状态栏深色字体
     *
     * @param activity
     */
    @SuppressLint("InlinedApi")
    public static void setStatusBarDarkMode(Activity activity) {
        int type = getStatusBarLightMode(activity);
        if (type == 1) {
            setMIUIStatusBarDarkMode(activity);
        } else if (type == 2) {
            setMeiZuDarkMode(activity.getWindow(), true);
        } else if (type == 3) {
            activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
    }


    /*----------------------------------------直接设置状态栏颜色为深色字体的方法,没有考虑小米和魅族---------------------------------------------------*/

    /**
     * 设置状态栏浅色字体的方法
     *
     * @param activity
     * @param isLightMode
     */
    public static void setStatusBarLightMode(@NonNull final Activity activity,
                                             final boolean isLightMode) {
        setStatusBarLightMode(activity.getWindow(), isLightMode);
    }

    public static void setStatusBarLightMode(@NonNull final Window window,
                                             final boolean isLightMode) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            View decorView = window.getDecorView();
            if (decorView != null) {
                int vis = decorView.getSystemUiVisibility();
                if (isLightMode) {
                    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                    vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
                } else {
                    vis &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
                }
                decorView.setSystemUiVisibility(vis);
            }
        }
    }


}
